<template>
  <el-row style="width:100%">
    <el-col :span="20">
      {{ nodeData.name }}
    </el-col>
    <el-col :span="4">
      <el-row :gutter="10">
        <el-col :span="12" style="text-align:right">{{ nodeData.manager || '暂无' }}</el-col>
        <el-col :span="12">
          <el-dropdown @command="handleClick">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="isRoot" command="edit">查看部门</el-dropdown-item>
              <el-dropdown-item v-if="isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown></el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDept } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      // 每一行叶子节点的数据
      type: Object,
      required: true
    },
    // 自定义的布尔值,控制查看和删除按钮的按需显示的
    isRoot: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    handleClick(command) {
      if (command === 'add') {
        // console.log(this.nodeData)
        // 通知父组件将 visible 控制对话框的布尔值改成true,并携带当前点击的部门信息
        this.$emit('showDialog', this.nodeData)
      }
      if (command === 'edit') {
        // console.log('查看部门')
        // 展示对话框
        this.$emit('editDialog', this.nodeData)
      }
      if (command === 'del') {
        // 1,确认消息提示
        this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
          type: 'warning'
        }).then(async() => {
          // 2,发送请求
          await reqDelDept(this.nodeData.id)
          // 3,提示 拦截器中实现
          // 4,子传父通知父组件 重新获取部门列表
          this.$emit('reload')
        }).catch(() => {
          this.$message.info('已取消操作')
        })
      }
    }
  }
}
</script>

<style>

</style>
